Skip to content

fix: filter embedding API key errors and harden beforeSend patterns#477

Merged
BYK merged 1 commit into
mainfrom
fix/sentry-embedding-apikey-filter
May 27, 2026
Merged

fix: filter embedding API key errors and harden beforeSend patterns#477
BYK merged 1 commit into
mainfrom
fix/sentry-embedding-apikey-filter

Conversation

@BYK
Copy link
Copy Markdown
Owner

@BYK BYK commented May 27, 2026

Summary

Fix three more Sentry issues and harden the beforeSend filter to suppress additional non-actionable errors.

LOREAI-GATEWAY-1G / 1H — OpenAI API 401 with placeholder key

Root cause: pickRemoteFallback() treated any truthy OPENAI_API_KEY as valid. Tools like Codex set OPENAI_API_KEY=nokey as a placeholder when routing through a custom OPENAI_BASE_URL. When the local embedding provider fails, the fallback used this placeholder key to make real requests to https://api.openai.com/v1/embeddings, producing 401 errors.

Fix: Added looksLikeApiKey() validation — keys shorter than 20 characters are rejected as placeholders, falling through to FTS-only search. Also added /Incorrect API key/i to the beforeSend filter as defense-in-depth.

LOREAI-GATEWAY-1A — getSystemErrorMap crash in Sentry SDK on Bun

Root cause: Bun doesn't implement getSystemErrorMap() from node:util. The Sentry SDK calls this during error processing, creating an infinite crash loop (original error → captureException → SDK internal processing → getSystemErrorMap crash → unhandled error).

Fix: Added /getSystemErrorMap/ to the beforeSend filter to suppress this Sentry SDK internal failure.

Additional beforeSend hardening

Added patterns for ONNX-related init failures that were slipping through on various platforms:

  • /onnxruntime/i — general ONNX runtime errors
  • /Cannot find package 'onnxruntime-node'/ — missing ONNX package (LOREAI-GATEWAY-18)
  • /LoadLibrary failed/ — Windows ONNX DLL load failure (LOREAI-GATEWAY-15)
  • /Protobuf parsing failed/ — corrupt/incompatible ONNX model file (LOREAI-GATEWAY-10)

Test Plan

  • All 1915 existing tests pass (1 new test added for placeholder key rejection)
  • Typecheck passes across all packages

Closes LOREAI-GATEWAY-1G, LOREAI-GATEWAY-1H, LOREAI-GATEWAY-1A

- Validate API keys in pickRemoteFallback() before using them for real
  API calls. Keys shorter than 20 characters (e.g. 'nokey' placeholder
  set by tools like Codex) are now rejected, falling through to FTS-only
  search instead of producing 401 errors against OpenAI.
  (Fixes LOREAI-GATEWAY-1G, LOREAI-GATEWAY-1H)

- Add beforeSend patterns for:
  - 'Incorrect API key' (defense-in-depth for embedding 401s)
  - 'onnxruntime' / 'onnxruntime-node' / 'LoadLibrary failed' /
    'Protobuf parsing failed' (ONNX init failures on various platforms)
  - 'getSystemErrorMap' (Bun doesn't implement node:util's
    getSystemErrorMap, which crashes the Sentry SDK during error
    processing — LOREAI-GATEWAY-1A)
@BYK BYK merged commit 3009563 into main May 27, 2026
7 checks passed
@BYK BYK deleted the fix/sentry-embedding-apikey-filter branch May 27, 2026 14:23
BYK added a commit that referenced this pull request May 27, 2026
## Summary

Addresses findings from the adversarial self-review of PR #475 and #477.

### Changes

**C1 — Unknown block types silently dropped in
`parseAnthropicResponseJSON`**

Added a `default` case to the content block switch that preserves
unknown block types as serialized JSON text. This matches the pattern
used in `toGatewayBlock()` for request parsing and prevents silent data
loss if Anthropic adds new content block types.

**C2 — Invalid scope values accepted by `filterOps()`**

Changed scope validation from `typeof o.scope === "string"` to `o.scope
=== "project" || o.scope === "global"`. Previously, an LLM producing an
invalid scope like `"session"` or `""` would pass validation and create
all entries with `projectPath: undefined` (global scope) regardless of
intent.

**M2 — `looksLikeApiKey()` doesn't trim or reject whitespace**

Added `key.trim()` and a `/\s/` check. Environment variables can contain
trailing newlines which would pass the length check but fail at the API.

**M4 — `/onnxruntime/i` pattern too broad**

Removed the overly broad `/onnxruntime/i` pattern from
`TRANSIENT_ERROR_PATTERNS`. The three specific patterns that follow it
(`Cannot find package`, `LoadLibrary failed`, `Protobuf parsing failed`)
already cover the known ONNX init failures without risking silencing
real bugs.

**M5 — `/Incorrect API key/i` could match unrelated auth errors**

Narrowed to `/Incorrect API key provided/i` which is the specific error
message format returned by the OpenAI SDK, avoiding false positives from
other auth error formats.

## Test Plan

- All 1915 tests pass
- Typecheck passes across all 4 packages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant